package ch.kerbtier.malurus.models;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class GenericListModel<T> implements ListModel<T>{
	private List<T> values = new ArrayList<T>();
	private List<ModelListener<T>> listeners = new ArrayList<ModelListener<T>>();

	public void add(T value) {
		values.add(value);
		callListeners();
	}

	private void callListeners() {
		for (ModelListener<T> ml : listeners) {
			ml.valueChanged(this);
		}
	}

	public T get(int pos) {
		return values.get(pos);
	}

	public void set(int pos, T value) {
		values.set(pos, value);
		callListeners();
	}

	public int size() {
		return values.size();
	}

	public Iterator<T> iterator() {
		return values.iterator();
	}

	public void addListener(ModelListener<T> listener) {
		listeners.add(listener);
	}

	public void removeListener(ModelListener<T> listener) {
		listeners.remove(listener);
	}

	public T remove(int pos) {
		T r = values.remove(pos);
		callListeners();
		return r;
	}
}
